package org.jeecg.modules.online.api.controller;


import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.online.cgform.service.impl.OnlineBaseExtAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 微服务化 online模块对外接口请求类
 */
@RestController("onlineBaseExtAPIController")
@RequestMapping("/online/api")
public class OnlineBaseExtAPIController {

    @Autowired
    OnlineBaseExtAPI onlineBaseExtAPI;

    /**
     * 【Online】 表单设计器专用：同步新增
     */
    @PostMapping(value = "/cgform/crazyForm/{name}")
    String cgformPostCrazyForm(@PathVariable("name") String tableName, @RequestBody JSONObject jsonObject) throws Exception {
        return onlineBaseExtAPI.cgformPostCrazyForm(tableName, jsonObject);
    }

    /**
     * 【Online】 表单设计器专用：同步编辑
     */
    @PutMapping(value = "/cgform/crazyForm/{name}")
    String cgformPutCrazyForm(@PathVariable("name") String tableName, @RequestBody JSONObject jsonObject) throws Exception {
        return onlineBaseExtAPI.cgformPutCrazyForm(tableName, jsonObject);
    }

    /**
     * 通过online表名查询数据，同时查询出子表的数据
     *
     * @param tableName online表名
     * @param dataIds   online数据ID
     * @return
     */
    @GetMapping(value = "/cgform/queryAllDataByTableName")
    JSONObject cgformQueryAllDataByTableName(@RequestParam("tableName") String tableName, @RequestParam("dataIds") String dataIds) {
        return onlineBaseExtAPI.cgformQueryAllDataByTableName(tableName, dataIds);
    }

    /**
     * online表单删除数据
     *
     * @param cgformCode Online表单code
     * @param dataIds    数据ID，可逗号分割
     * @return
     */
    @DeleteMapping("/cgform/cgformDeleteDataByCode")
    String cgformDeleteDataByCode(@RequestParam("cgformCode") String cgformCode, @RequestParam("dataIds") String dataIds) {
        return onlineBaseExtAPI.cgformDeleteDataByCode(cgformCode, dataIds);
    }

    /**
     * 【cgreport】通过 head code 获取 sql语句，并执行该语句返回查询数据
     *
     * @param code     报表Code，如果没传ID就通过code查
     * @param forceKey
     * @param dataList
     * @return
     */
    @GetMapping("/cgreportGetData")
    Map<String, Object> cgreportGetData(@RequestParam("code") String code, @RequestParam("forceKey") String forceKey, @RequestParam("dataList") String dataList) {
        return onlineBaseExtAPI.cgreportGetData(code, forceKey, dataList);
    }

    /**
     * 【cgreport】对 cgreportGetData 的返回值做优化，封装 DictModel 集合
     *
     * @return
     */
    @GetMapping("/cgreportGetDataPackage")
    List<DictModel> cgreportGetDataPackage(@RequestParam("code") String code, @RequestParam("dictText") String dictText, @RequestParam("dictCode") String dictCode, @RequestParam("dataList") String dataList) {
        return onlineBaseExtAPI.cgreportGetDataPackage(code, dictText, dictCode, dataList);
    }

}
